#include "lyricview.h"
#include "QListWidget"
#include "QVBoxLayout"
#include <memory> // 包含智能指针的头文件
#include <QList>
#include <QLabel>
#include <QDialog>
#include <QDesktopWidget>
#include <QApplication>


LyricView::LyricView(QWidget *parent) :
    QWidget(parent), listWidget(new QListWidget(this)), layout(new QVBoxLayout(this)), lyricItemList(new QList<LyricItem*>),currentIndex(0),desktopLyricDialog(new DesktopLyricDialog(this))
{

    // 设置 ListWidget 的大小和位置
    listWidget->resize(200, 300);
    listWidget->move(50, 50);

    // 添加 ListWidget 到布局中
    layout->addWidget(listWidget);
    this->setLayout(layout);

    //监听对话框关闭
    connect(this->desktopLyricDialog, &QDialog::rejected, this, &LyricView::onDesktopLyricDialogClosed);
}

//处理歌词
void LyricView::setLyric(QString lyrics)
{
    QStringList lines = lyrics.split("\n", QString::SkipEmptyParts);
    int index=0;
    foreach (QString line, lines) {
        QString time=line.left(11);
        QString content = line.mid(11);
        LyricItem *lyrics = new LyricItem(index,time,content);
        index++;
        this->lyricItemList->append(lyrics);

        QLabel *label = new QLabel(content);

        // 设置字体大小
        QFont font("Arial", 15);  // 设置字体和字号
        label->setFont(font);
        // 设置字体颜色
        label->setStyleSheet("color: black;");  // 设置颜色为黑色

        QListWidgetItem *item = new QListWidgetItem();
        // 将 QLabel 设置为 QListWidgetItem 的小部件
        item->setSizeHint(label->sizeHint());
        listWidget->addItem(item);
        listWidget->setItemWidget(item, label);
        // 设置 QListWidget 的间距
        listWidget->setSpacing(5);  // 设置间距为10像素
    }

    QListWidgetItem *firstItem = listWidget->item(0); // 第一个 item 的索引
    QWidget *widget = listWidget->itemWidget(firstItem);
    QLabel *label = qobject_cast<QLabel*>(widget);
    label->setStyleSheet("color: red;");
}

void LyricView::scrollToItemByTime(QString time)
{
    int targetIndex = -1;
    foreach(LyricItem *item,*lyricItemList){
        if(timeStringToMillis(time) < timeStringToMillis(item->time)){
            targetIndex=item->index - 1;
            break;
        }
    }

    // 使用 scrollToItem 方法滚动到指定的 item
    QListWidgetItem *itemToScrollTo = listWidget->item(targetIndex);
    if (itemToScrollTo) {
        listWidget->scrollToItem(itemToScrollTo, QAbstractItemView::PositionAtCenter);
        //将上一句歌词还原成正常颜色
        QListWidgetItem *item = listWidget->item(this->currentIndex);
        QWidget *widget = listWidget->itemWidget(item);
        QLabel *label = qobject_cast<QLabel*>(widget);
        label->setStyleSheet("color: black;");
        //把下一句歌词变色
        QListWidgetItem *item2 = listWidget->item(targetIndex);
        QWidget *widget2 = listWidget->itemWidget(item2);
        QLabel *label2 = qobject_cast<QLabel*>(widget2);
        label2->setStyleSheet("color: red;");
        //更新this->currentIndex
        this->currentIndex = targetIndex;

        //更新桌面歌词
        desktopLyricDialog->updateLyric(lyricItemList->at(targetIndex)->content);
    }
}

// 函数用于将时间格式的QString转换为总毫秒数
qint64 LyricView::timeStringToMillis(const QString &timeString)
{
    QRegularExpression regex("\\[(\\d{2}):(\\d{2})\\.(\\d{3})\\]");
    QRegularExpressionMatch match = regex.match(timeString);
    if (match.hasMatch()) {
        int minutes = match.captured(1).toInt();
        int seconds = match.captured(2).toInt();
        int milliseconds = match.captured(3).toInt();
        return minutes * 60000 + seconds * 1000 + milliseconds;
    }
    return 0;  // 如果格式不匹配，返回0
}

void LyricView::desktopLyric()
{
    this->desktopLyricShow = !this->desktopLyricShow;
    if(this->desktopLyricShow){
        QPoint currentPos = this->desktopLyricDialog->pos(); // 获取当前对话框位置
        this->desktopLyricDialog->move(currentPos.x(), currentPos.y());// 指定对话框显示在 (x, y) 的位置
        this->desktopLyricDialog->show();
    }else{
        this->desktopLyricDialog->hide();
    }

}

LyricView::~LyricView()
{
    desktopLyricDialog->close();
    qDeleteAll(*lyricItemList); // Delete all LyricItem objects in lyricItemList
    lyricItemList->clear();
    delete lyricItemList;
    delete layout;
    delete listWidget;
    delete desktopLyricDialog;
}

void LyricView::onDesktopLyricDialogClosed()
{
    this->desktopLyricShow = false;
}

void LyricView::closeLyricDialog()
{
    this->desktopLyricDialog->close();
}

LyricItem::LyricItem(int index,QString time,QString content)
{
    this->index=index;
    this->time=time;
    this->content=content;
}

LyricItem::~LyricItem()
{

}


void DesktopLyricDialog::updateLyric(QString lyric)
{
    this->desktopLyricLabel->setText(lyric);
}

DesktopLyricDialog::DesktopLyricDialog(QWidget *parent)
{
    this->setWindowTitle("桌面歌词");
    this->setWindowFlags(this->windowFlags() | Qt::Tool);
    //桌面歌词对话框
    desktopLyricLabel = new QLabel("桌面歌词   听音乐，就听IKUN！", this);
    // 设置字体颜色
    desktopLyricLabel->setStyleSheet("font-weight: 1000; font-family: Arial, Helvetica, sans-serif; font-size: 28pt;color: blue;");  // 设置颜色

    desktopLyricLayout = new QVBoxLayout(this);
    desktopLyricLayout->addWidget(desktopLyricLabel,0, Qt::AlignCenter);
    setLayout(desktopLyricLayout);

    int dialogWidth = 800;
    int dialogHeight = 200;

    resize(dialogWidth, dialogHeight);
    setAttribute(Qt::WA_TranslucentBackground,true); // 设置窗口背景透明
    setWindowFlags(this->windowFlags() | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint); // 隐藏窗口边框


    // 获取屏幕的中心位置
    QDesktopWidget *desktop = QApplication::desktop();
    int screenWidth = desktop->width();
    int screenHeight = desktop->height();


    int x = (screenWidth - dialogWidth) / 2 ;
    int y = (screenHeight - dialogHeight) / 2;
    move(x,y);
    delete desktop;
}

DesktopLyricDialog::~DesktopLyricDialog()
{
    delete desktopLyricLabel;
    delete desktopLyricLayout;
}

void DesktopLyricDialog::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        // 记录鼠标相对于窗口的位置
        dragStartPosition = event->globalPos() - frameGeometry().topLeft();
        event->accept();
    }
}

void DesktopLyricDialog::mouseMoveEvent(QMouseEvent *event)
{
    if (event->buttons() & Qt::LeftButton) {
         // 移动窗口
         move(event->globalPos() - dragStartPosition);
         event->accept();
    }
}


